package com.wxr.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {



    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人可以访问，功能也只有对应有权限的人才能访问
        //请求授权的规则
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        //没有权限跳转到登录页面,需要开启登录的页面
        http.formLogin();
        //防止网站攻击,关闭，get是明文传输，不安全，被拦截掉了。
        http.csrf().disable();
        //开启注销功能
        http.logout().logoutSuccessUrl("/");
        //开启记住我功能
        http.rememberMe();

    }
    //认证
    //密码编码 passwordEncoder
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new Pbkdf2PasswordEncoder())
                .withUser("wxr").password(new Pbkdf2PasswordEncoder().encode("123456")).roles("vip2","vip3")
                .and()
                .withUser("root").password(new Pbkdf2PasswordEncoder().encode("123456")).roles("vip1","vip2","vip3");
    }
}
